package com.cykj.pos.service.impl;

import com.cykj.common.core.domain.AjaxResult;
import com.cykj.pos.domain.BizMerchant;
import com.cykj.pos.service.IBizMerchantService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.AizStatisticalMerchantMapper;
import com.cykj.pos.domain.AizStatisticalMerchant;
import com.cykj.pos.service.IAizStatisticalMerchantService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 代理渠道交易收益统计Service业务层处理
 *
 * @author xxl
 * @date 2025-03-21
 */
@Service
public class AizStatisticalMerchantServiceImpl extends ServiceImpl<AizStatisticalMerchantMapper, AizStatisticalMerchant> implements IAizStatisticalMerchantService {

    @Resource
    private AizStatisticalMerchantMapper aizStatisticalMerchantMapper;

    @Resource
    private IBizMerchantService iBizMerchantService;

    @Override
    public List<AizStatisticalMerchant> queryList(AizStatisticalMerchant aizStatisticalMerchant) {
        LambdaQueryWrapper<AizStatisticalMerchant> lqw = Wrappers.lambdaQuery();
        if (aizStatisticalMerchant.getTotalProfitAmount() != null){
            lqw.eq(AizStatisticalMerchant::getTotalProfitAmount ,aizStatisticalMerchant.getTotalProfitAmount());
        }
        if (aizStatisticalMerchant.getYearProfitAmount() != null){
            lqw.eq(AizStatisticalMerchant::getYearProfitAmount ,aizStatisticalMerchant.getYearProfitAmount());
        }
        if (aizStatisticalMerchant.getLastMonthProfitAmount() != null){
            lqw.eq(AizStatisticalMerchant::getLastMonthProfitAmount ,aizStatisticalMerchant.getLastMonthProfitAmount());
        }
        if (aizStatisticalMerchant.getThisMonthProfitAmount() != null){
            lqw.eq(AizStatisticalMerchant::getThisMonthProfitAmount ,aizStatisticalMerchant.getThisMonthProfitAmount());
        }
        if (aizStatisticalMerchant.getTodayProfitAmount() != null){
            lqw.eq(AizStatisticalMerchant::getTodayProfitAmount ,aizStatisticalMerchant.getTodayProfitAmount());
        }
        if (aizStatisticalMerchant.getTotalTransAmount() != null){
            lqw.eq(AizStatisticalMerchant::getTotalTransAmount ,aizStatisticalMerchant.getTotalTransAmount());
        }
        if (aizStatisticalMerchant.getYearTransAmount() != null){
            lqw.eq(AizStatisticalMerchant::getYearTransAmount ,aizStatisticalMerchant.getYearTransAmount());
        }
        if (aizStatisticalMerchant.getLastMonthTransAmount() != null){
            lqw.eq(AizStatisticalMerchant::getLastMonthTransAmount ,aizStatisticalMerchant.getLastMonthTransAmount());
        }
        if (aizStatisticalMerchant.getThisMonthTransAmount() != null){
            lqw.eq(AizStatisticalMerchant::getThisMonthTransAmount ,aizStatisticalMerchant.getThisMonthTransAmount());
        }
        if (aizStatisticalMerchant.getTodayTransAmount() != null){
            lqw.eq(AizStatisticalMerchant::getTodayTransAmount ,aizStatisticalMerchant.getTodayTransAmount());
        }
        if (aizStatisticalMerchant.getYesterdayTransAmount() != null){
            lqw.eq(AizStatisticalMerchant::getYesterdayTransAmount ,aizStatisticalMerchant.getYesterdayTransAmount());
        }
        if (aizStatisticalMerchant.getYesterdayProfitAmount() != null){
            lqw.eq(AizStatisticalMerchant::getYesterdayProfitAmount ,aizStatisticalMerchant.getYesterdayProfitAmount());
        }
        if (aizStatisticalMerchant.getChannelId() != null){
            lqw.eq(AizStatisticalMerchant::getChannelId ,aizStatisticalMerchant.getChannelId());
        }
        return this.list(lqw);
    }

    @Override
    public List<AizStatisticalMerchant> queryAllList() {
        return aizStatisticalMerchantMapper.queryAllList();
    }

    @Override
    public AizStatisticalMerchant getAizStatisticalsByUserIdAndChannel(Long userId, Long channelId) {
        return aizStatisticalMerchantMapper.getAizStatisticalsByUserIdAndChannel(userId,channelId);
    }

    @Override
    public void updateTransAmount(AizStatisticalMerchant asm) {
        aizStatisticalMerchantMapper.updateTransAmount(asm);
    }

    @Override
    public AjaxResult merchantChannelStatistic() {
        // 处理按渠道统计的交易量
        List<AizStatisticalMerchant> aizList = aizStatisticalMerchantMapper.queryAllList();
        for(AizStatisticalMerchant asm : aizList){
            BizMerchant bizMerchant = iBizMerchantService.getMerchantByUserId(asm.getUserId());
            if(bizMerchant == null){
                continue;
            }
            System.out.println(asm.getId());
            System.out.println(bizMerchant.getMerchName());
            // 渠道 , 代理 , 自身及团队
            BigDecimal lastTransAmount = aizStatisticalMerchantMapper.getLastTransAmount(bizMerchant.getMerchId(),asm.getChannelId());
            asm.setLastMonthTransAmount(lastTransAmount.doubleValue());
            System.out.println("lastTransAmount"+lastTransAmount);
            BigDecimal thisTransAmount = aizStatisticalMerchantMapper.getThisTransAmount(bizMerchant.getMerchId(),asm.getChannelId());
            asm.setThisMonthTransAmount(thisTransAmount.doubleValue());
            System.out.println("thisTransAmount"+thisTransAmount);
            // fenrun
            BigDecimal lastProfitAmount = aizStatisticalMerchantMapper.getLastProfitAmount(bizMerchant.getMerchId(),asm.getChannelId());
            asm.setLastMonthProfitAmount(lastProfitAmount.doubleValue());
            System.out.println("lastProfitAmount"+lastProfitAmount);
            BigDecimal thisProfitAmount = aizStatisticalMerchantMapper.getThisProfitAmount(bizMerchant.getMerchId(),asm.getChannelId());
            asm.setThisMonthProfitAmount(thisProfitAmount.doubleValue());
            System.out.println("thisProfitAmount"+thisProfitAmount);
            aizStatisticalMerchantMapper.updateTransAmount(asm);
        }
        return AjaxResult.success();
    }
}
